package cn.e4j.modular.api.controller;

import cn.digiwin.wx.utils.WeixinHandler;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.service.FwpgService;
import cn.e4j.modular.api.service.FwzpService;
import cn.e4j.modular.api.service.Gwat200Service;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.system.entity.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by caijy on 2019/08/26.
 */
@RestController
@RequestMapping("api/wx/fwzp")
public class FwzpController extends ControllerSupport {

    private static Logger logger = LoggerFactory.getLogger(FwzpController.class);

    @Autowired
    private FwzpService fwzpService;

    @Autowired
    Gwat200Service gwat200Service;

    @Autowired
    FwpgService fwpgService;

    /**
     * 服务转排
     * @return
     */
    @RequestMapping(value = "getServiceOrderListData")
    public ResponseData getServiceOrderListData(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<>();
        SysUser currentUser = wapper.getSysUser();
        try {
//            / 获取区域权限-进行省市过滤数据
            inDto.put("GWAA003", currentUser.getACCOUNT());
            inDto.put("MMRE004S",gwat200Service.getMmreData(inDto));

            inDto.put("MMRD004", DateUtils.parseDate(inDto.getString("MMRD004")));
            list = fwzpService.getServiceOrderListData(wapper.getPage(),inDto);
        } catch (Exception e) {
            e.printStackTrace();
           return ERROR;
        }
        return SUCCESS(list);
    }

    @RequestMapping(value = "saveFwpg")
    public ResponseData saveFwpg(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<>();
        try {
            list = fwzpService.saveFwpg(wapper.getSysUser(),inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }

        try {
            if (ToolUtil.isNotEmpty(list)){
                list.forEach(dto->{
                    //品名
                    String gwaml003 = dto.getString("GWAML003");
                    String mmrf008 = dto.getString("mmrf008");
                    String gwakl004 = dto.getString("gwakl004");
                    if (ToolUtil.isEmpty(gwaml003))
                        logger.error("消息中品名获取为空！");
                    else {
                        String userId = dto.getString("pguser");
                        //派工人员
                        Dto pgDto = Dtos.newDto();
                        pgDto.put("userid", userId);
                        Dto gwaa2 = fwpgService.getGwaa2(pgDto);

                        if (ToolUtil.isEmpty(gwaa2))
                            logger.error("消息中派工人员获取为空！");
                        else {
                            //派工消息
                            String pgmsg = String.format("设备[%s]已安排您去维修，请注意！",gwaml003);
                            WeixinHandler.sendMsg(gwaa2,pgmsg);

                            String gwaa002 = gwaa2.getString("NAME");
                            String gwaa004 = gwaa2.getString("GWAA004");
                            //预约者
                            String yyuser = dto.getString("yyuser");

                            if (ToolUtil.isEmpty(yyuser))
                                logger.error("消息中预约者获取为空！");
                            else {
                                Dto yyDto = new Dto();
                                yyDto.put("GWAA001",yyuser);
                                Dto gwaa21 = fwpgService.getGwaa2(yyDto);

                                if (ToolUtil.isNotEmpty(gwaa21) && gwaa002!=null){
                                    //预约者消息
                                    String msg = String.format("[%s]客户的[%s]设备已经安排[%s]工程师电话[%s]去服务，预计上门时间为[%s]！",gwakl004,gwaml003,gwaa002,gwaa004,mmrf008);
                                    WeixinHandler.sendMsg(gwaa21,msg);
                                }
                            }

                        }
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return SUCCESS;
    }

}
